package com.ruoyi.flowable.listener.move;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.equ.domain.EquMove;
import com.ruoyi.equ.service.IEquMoveService;
import com.ruoyi.flowable.service.IWorkOrderAssetRelService;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 移动-驳回监听
 */
@Slf4j
@Component("moveRejectListener")
public class MoveRejectListener implements ExecutionListener {
    private static final IEquMoveService moveService = SpringUtils.getBean(IEquMoveService.class);
    private static final IWorkOrderAssetRelService workOrderAssetRelService = SpringUtils.getBean(IWorkOrderAssetRelService.class);


    @Override
    public void notify(DelegateExecution execution) {
        String assetName = execution.getVariable("assetName", String.class);
        String moveType = execution.getVariable("moveType", String.class);
        EquMove equMove = execution.getVariable("equMove", EquMove.class);
        equMove.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        equMove.setRemark(assetName + moveType);
        moveService.insertEquMove(equMove);

        //更新工单状态(流程实例和资产结合的工单)
        String processInstanceId = execution.getProcessInstanceId();
        workOrderAssetRelService.updateWorkOrderStatus(processInstanceId, "已驳回", new Date());
    }
}
